package com.ys.zero.dao;

import java.util.List;
import java.util.Map;

import com.ys.zero.bean.Comment;
import com.ys.zero.bean.User;
import com.ys.zero.util.DBHelper;

/**
 * 评论Dao
 * @author Administrator
 *
 */
public class CommentDao {

	public void insertCommment(String cuaid, String account, String content) {
		try {
			String sql = "INSERT INTO `comment` VALUES(null,?,?,NOW(),?,0,0,null)";
			String sql1 = "update uarticle set chat = chat + 1 where uaid = ?";
			DBHelper.update(sql,cuaid,account,content);
			DBHelper.update(sql1,cuaid);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Map<String, Object>> queryComment(String cuaid) {
		List<Map<String, Object>> list =null;
		try {
			String sql = "SELECT  `user`.id,`user`.image,`user`.`name`,`comment`.`coaccount`,`comment`.`comment`,`comment`.time,`comment`.cpraise from comment,user where comment.coaccount = user.account  and comment.cuaid = ? order by comment.time desc";
			list =DBHelper.selectList(sql, cuaid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public void del(String cuaid, String coaccount, String comment) {
		try {
			String sql = "DELETE FROM comment where cuaid=? and coaccount = ? and comment = ?";
			DBHelper.update(sql,cuaid,coaccount,comment);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void dz(String cuaid, String coaccount, String comment) {
		try {
			String sql = "UPDATE comment SET cpraise = cpraise+1 WHERE cuaid=? and coaccount = ? and comment = ?";
			DBHelper.update(sql,cuaid,coaccount,comment);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void qxdz(String cuaid, String coaccount, String comment) {
		try {
			String sql = "update comment set cpraise = cpraise-1 where cuaid=? and coaccount = ? and comment = ?";
			DBHelper.update(sql,cuaid,coaccount,comment);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void hcomment(String huaid, String bhaccount, String haccount, String hcomment) {	
		try {
			String sql = "INSERT INTO comment_reply VALUES(null,?,?,?,?,now(),0)";
			DBHelper.update(sql,huaid,bhaccount,haccount,hcomment);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void hshowComment(String huaid, String bhaccount, String hcomment) {	
		try {
			String sql = "SELECT  * from user,comment_reply where comment_reply.haccount = user.account  "
					+ " and comment_reply.huaid = ? and comment_reply.bhaccount=? "
					+ " and comment_reply.hcomment=?";
			DBHelper.update(sql,huaid,bhaccount,hcomment);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Comment> queryMyComment(String coaccount) {
		String sql = "select * from comment where coaccount=?";
		return DBHelper.query(sql,Comment.class,coaccount);
	}
	
	public void delMyComment(String cid) {
		try {
			String sql = "delete from comment where cid = ?";
			DBHelper.update(sql, cid);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
